import { _decorator, Component, log, Vec2, Vec3, Node, UIOpacity, Label, tween, Tween } from 'cc';
const { ccclass, property } = _decorator;

import { GobangBoard } from "./GobangBoard";
import { NONE, BLACK, WHITE } from "./GobangConstants";
import { GobangAI } from "./GobangAI";

@ccclass('GobangScene')
export class GobangScene extends Component {
    @property(GobangBoard)
    private board: GobangBoard = null;
    @property(Node)
    private spTips: Node = null;
    @property(Node)
    private tips: Node = null;
    private ai: GobangAI = null;
    private state = NONE;
    private tipsOpacity: UIOpacity;
    start() {
        this.board.init(this);
        this.ai = new GobangAI(this.board);

        this.tipsOpacity = this.spTips.getComponent(UIOpacity);
        this.startGame();
    }
    startGame() {
        this.state = BLACK;
        this.board.reset();
        // G.gameRoot.showTip("你是黑棋你先走");
        this.showTip("你是黑棋你先走");
    }
    showTip(tips: string){
        Tween.stopAllByTarget(this.tipsOpacity);
        this.spTips.active = true;
        this.tipsOpacity.opacity = 255;
        this.tips.getComponent(Label).string = tips;
        tween(this.tipsOpacity).to(1.5, { opacity: 0 }, { easing: "quintIn" }).call(() => {
            this.spTips.active = false;
        }).start();
    }
    overGame() {
        if (this.state === BLACK) {
            log("黑棋胜");
            this.state = NONE;
            this.showTip("你赢了阿尔法二狗！");
            // G.gameRoot.showMaskMessage("你赢了阿尔法二狗！",
            //     { label: "朕知道了", cb: () => { }, target: this });
        } else if (this.state === WHITE) {
            log("白旗胜");
            this.showTip("你输给了阿尔法二狗！");
            // G.gameRoot.showMaskMessage("你输给了阿尔法二狗！",
            //     { label: "服了", cb: () => { }, target: this },
            //     { label: "不服", cb: () => { }, target: this });
            this.state = NONE;
        }
    }
    onBtnReturn() {
        // G.returnHall();
    }
    onBtnRestart() {
        this.startGame();
    }
    onBoardTouched(coor: Vec3) {
        switch (this.state) {
            case NONE:

                break;
            case BLACK:
                let piece = this.board.getPieceByCoor(coor);
                if (piece.color !== NONE) {
                    // G.gameRoot.showTip("这里有子了，你是不是傻");
                    this.showTip("这里有子了，你是不是傻");
                    return;
                }
                this.board.placeBlack(coor);
                if (this.board.judgeWin()) {
                    this.overGame();
                } else {
                    this.state = WHITE;
                    this.scheduleOnce(() => {
                        this.board.placeWhite(this.ai.getNextCoor());
                        if (this.board.judgeWin()) {
                            this.overGame();
                        } else {
                            this.state = BLACK;
                        }
                    }, 1);
                }
                break;
            case WHITE:
                // this.board.placeWhite(coor);
                // if(this.board.judgeWin()) {
                //     cc.log("白棋胜");
                // }else {
                //     this.state = BLACK;
                // }
                break;
            default: break;
        }
    }
}

